/*
 * Copyright 2010 Marcin Blazejewski
 *
 * This file is part of Nofate.
 *
 * Nofate is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Nofate is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Nofate. If not, see <http://www.gnu.org/licenses/>.
 *
 */


#ifndef GENESIS_H_
#define GENESIS_H_

#include <vector>

using namespace std;

class F2 {
private:
    void level_step(F2 &mask, int x0, int y0, float range, float v0);

public:
    float **data;
    unsigned int width;
    unsigned int height;

    F2(unsigned int w, unsigned int h);
    F2(const F2 &r);
    ~F2();

    void fill(float v);
    void fill(float v, int x0, int y0, int x1, int y1);
    void randomize(float v0, float v1, int m=10000);
    void normalize();
    
    float avg(); // deprecated
    float get_avg();
    float get_max(int *rx=NULL, int *ry=NULL);
    float get_min(int *rx=NULL, int *ry=NULL);

    int count(float v0, float range=0);
    int find_maxhline(int y0, float v0, float v1, int &rx0, int &rx1);

    int find_maxvline(int x0, float v0, float v1, int &ry0, int &ry1);
    void find_clearing(float v0, float v1, int &rx, int &ry);

    void blend(F2 &r, float ratio=0.5f, int x0=0, int y0=0);
    void stamp(F2 &r, F2 &orig);
    void level(int x0, int y0, float range);
    void smooth(int x0=0, int y0=0, int x1=0, int y1=0);
};


void perlin_noise(F2 &src, F2 &data, int ofsx, int ofsy, int N = 1, float P = 0.5f);
void automata(F2 &data, int N=100000);
void blob(F2 &data, int R, int span, float v, int res=32, int x0=-1, int y0=-1);

int randv(int x0, int x1, int lead_percent=0, int tail_percent=0);

#endif
